

package pos.util;

import pos.exception.POSException;
import pos.interfaces.Messages;
import java.util.regex.*;

/**
 * This method is used for validate the payment information.
 * @author wei zhang
 */
public class paymentValidate implements Messages {

   /**
     * Check if the payment card number is valid
     * @param inputText
     * @return
     * @throws POSException
     */
    public String validPaymentCardNumber (String inputText) throws POSException{

        Pattern paymentCardNum = Pattern.compile("[\\d]{16,19}");
        Matcher matchPaymentCardNum;

        validTextInput(inputText);

        matchPaymentCardNum = paymentCardNum.matcher(inputText.trim());

        if(!matchPaymentCardNum.matches()){
            throw new POSException(Messages.validPaymentCard);
        }
        return inputText;
    }
       /**
    * Check if the month is valid
    * @param inputText
    * @return
    * @throws POSException
    */
    public String validMonth (String inputText) throws POSException{

        Pattern month = Pattern.compile("[\\d]{2}");
        Matcher matchMonth;

        validTextInput(inputText);

        matchMonth = month.matcher(inputText.trim());

        if(!matchMonth.matches()){
            throw new POSException(Messages.validMonth);
        }
        return inputText;
    }
   /**
    * Check if the year is valid
    * @param inputText
    * @return
    * @throws POSException
    */
    public String validYear (String inputText) throws POSException{

        Pattern year = Pattern.compile("[\\d]{4}");
        Matcher matchYear;

        validTextInput(inputText);

        matchYear = year.matcher(inputText.trim());

        if(!matchYear.matches()){
            throw new POSException(Messages.validYear);
        }
        return inputText;
    }
   /**
    * Check if the payment card security number is valid
    * @param inputText
    * @return
    * @throws POSException
    */
    public String validCardSecurityNumber (String inputText) throws POSException{

        Pattern CardSecurityNum = Pattern.compile("[\\d]{3}");
        Matcher matchCardSecurityNum;

        validTextInput(inputText);

        matchCardSecurityNum = CardSecurityNum.matcher(inputText.trim());

        if(!matchCardSecurityNum.matches()){
            throw new POSException(Messages.validCardSecurityNumber);
        }
        return inputText;
    }
    
     /**
    * Check if the input text is not empty or null
    * @param inputText
    * @return
    * @throws POSException
    */
    public String validTextInput(String inputText) throws POSException{
        if(inputText == null){
            throw new POSException(Messages.NULLVALE);
        }
        else {
            inputText = inputText.trim();
            if(inputText.isEmpty()){
                throw new POSException(Messages.NULLVALE);
            }
        }
        return inputText;
    }

}
